package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.climbtower.*;
import com.yanqu.road.entity.activity.climbtower.enums.eClimbTowerEventType;
import com.yanqu.road.entity.log.LogClimbTowerTopFight;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.ClimbTowerProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.utils.UUIDHelper;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class ClimbTowerActivityPb {

    public static ClimbTowerProto.ClimbTowerUserDataTemp.Builder buildUserData(UserClimbTowerData userData, ClimbTowerRoleInfo roleInfo, boolean isInTop) {
        ClimbTowerProto.ClimbTowerUserDataTemp.Builder builder = ClimbTowerProto.ClimbTowerUserDataTemp.newBuilder();
        builder.setLv(roleInfo.getLevel());
        builder.setExperience(userData.getExp().toString());
        builder.setPower(userData.getPower());
        builder.setLastRecoverPowerTime(userData.getLastRecoverPowerTime());
        builder.setScore(userData.getScore());
        builder.setAbility(userData.getAbility().toString());
        builder.setMultiple(roleInfo.getAddEffect());
        builder.setCellNum(userData.getCellNum());

        ClimbTowerProto.ClimbTowerEventDataTemp.Builder event = ClimbTowerProto.ClimbTowerEventDataTemp.newBuilder();
        event.setPassId(userData.getEvent().getPassId());
        event.setCell(userData.getEvent().getCell());
        event.setColIndex(userData.getEvent().getColIndex());
        builder.setEvent(event);

        builder.setIsInTopCell(isInTop);
        return builder;
    }


    public static ClimbTowerProto.ClimbTowerRoleConfigTemp.Builder buildRoleConfig(ClimbTowerRoleInfo roleInfo) {
        ClimbTowerProto.ClimbTowerRoleConfigTemp.Builder builder = ClimbTowerProto.ClimbTowerRoleConfigTemp.newBuilder();
        builder.setId(roleInfo.getId());
        builder.setLevel(roleInfo.getLevel());
        builder.setExp(roleInfo.getExp().toString());
        builder.setAddEffect(roleInfo.getAddEffect());
        builder.setPic(roleInfo.getPic());
        builder.setArmPic(roleInfo.getArmsPic());
        return builder;
    }

    public static List<ClimbTowerProto.ClimbTowerPassCellTemp.Builder> buildLevelConfig(ClimbTowerLevelInfo levelInfo, ClimbTowerConfig config, List<Integer> handledList) {
        List<ClimbTowerProto.ClimbTowerPassCellTemp.Builder> list = new ArrayList<>();
        for (int i = 0; i < levelInfo.getCellList().size(); i++) {
            ClimbTowerCell towerCell = levelInfo.getCellList().get(i);
            ClimbTowerProto.ClimbTowerPassCellTemp.Builder builder = ClimbTowerProto.ClimbTowerPassCellTemp.newBuilder();
            builder.setPassId(levelInfo.getId());
            builder.setCellId(i);
            builder.setCellType(levelInfo.getCellTypeList().get(i));
            builder.setTemplateId(levelInfo.getTemplateId());
            builder.setCellNum(towerCell.getCellNum());
            for (ClimbTowerCellEvent cellEvent : towerCell.getEventList()) {
                ClimbTowerProto.ClimbTowerCellEventTemp.Builder eventBuilder = ClimbTowerProto.ClimbTowerCellEventTemp.newBuilder();
                eventBuilder.setEventId(cellEvent.getEventType());
                eventBuilder.setEventType(cellEvent.getEventType());
                eventBuilder.setEventVal(cellEvent.getEventVal());
                eventBuilder.setPic(cellEvent.getPic());
                eventBuilder.setRawVal(cellEvent.getRawVal());
                if(cellEvent.getEventType() == eClimbTowerEventType.Weapons.getType() && handledList.contains(towerCell.getCellNum())){
                    eventBuilder.setHadHandle(true);
                }
                builder.addEvent(eventBuilder);
            }
            builder.setBossID(levelInfo.getBossId());
            list.add(builder);
        }
        return list;
    }

    public static void buildOtherConfig(ClimbTowerConfig config, ClimbTowerProto.ClimbTowerConfigSyncMsg.Builder configSync) {

        for (ClimbTowerRoleInfo roleInfo : config.getRoleInfoList()) {
            configSync.addRole(ClimbTowerActivityPb.buildRoleConfig(roleInfo));
        }

        for (ClimbTowerEventInfo eventInfo : config.getEventInfoList()) {
            ClimbTowerProto.ClimbTowerEventTemp.Builder builder = ClimbTowerProto.ClimbTowerEventTemp.newBuilder();
            builder.setId(eventInfo.getId());
            builder.setType(eventInfo.getType());
            builder.setLittleType(eventInfo.getLittleType());
            builder.setExParam(eventInfo.getExParam());
            configSync.addEvent(builder);
        }

        for (ClimbTowerMultipleInfo multipleInfo : config.getMultipleInfoList()) {
            ClimbTowerProto.ClimbTowerDrawMultipleTemp.Builder builder = ClimbTowerProto.ClimbTowerDrawMultipleTemp.newBuilder();
            builder.setId(multipleInfo.getId());
            builder.setValue(multipleInfo.getValue());
            builder.setParam(multipleInfo.getParam());
            builder.setWeight(multipleInfo.getWeight());
            configSync.addMultiple(builder);
        }

        for (ClimbTowerTemplateInfo templateInfo : config.getTemplateInfoList()) {
            ClimbTowerProto.ClimbTowerTemplateTemp.Builder builder = ClimbTowerProto.ClimbTowerTemplateTemp.newBuilder();
            builder.setId(templateInfo.getId());
            builder.addAllCodeA(StringUtils.stringToIntegerList(templateInfo.getCodeA(), "\\|"));
            configSync.addTemplate(builder);
        }

    }

    public static UserClimbTowerTopClimbRecord buildTopClimbRecord(ClimbTowerProto.CrossClimbTowerTopClimbRespMsg msg) {
        UserBaseInfo enemyBaseInfo = PlayerBasePb.parseBaseUserInfo(msg.getEnemyBaseInfo());
        UserClimbTowerTopClimbRecord data = new UserClimbTowerTopClimbRecord();
        data.setInsertOption();
        data.setRecordId(UUIDHelper.randomUUID(String.valueOf(GameServer.getInstance().getServerId())));
        data.setActivityId(msg.getActivityId());
        data.setUserId(msg.getSelfUserId());
        data.setAbility(new BigInteger(msg.getSelfAbility()));
        data.setEnemyUserId(msg.getEnemyUserId());
        data.setEnemyAbility(new BigInteger(msg.getEnemyAbility()));
        data.setEnemyBaseInfo(enemyBaseInfo);
        data.setOldCellNum(msg.getRetClient().getOldTopCell());
        data.setNewCellNum(msg.getRetClient().getNewTopCell());
        data.setRecordTime(DateHelper.getCurrentTime());

        return data;
    }

    public static ClimbTowerProto.ClimbTowerTopClimbRespMsg.Builder builderTopClimbRespMsg() {
        ClimbTowerProto.ClimbTowerTopClimbRespMsg.Builder builder = ClimbTowerProto.ClimbTowerTopClimbRespMsg.newBuilder();
        builder.setRet(0);
        builder.setOldTopCell(0);
        builder.setNewTopCell(0);
        builder.setMaxTopCell(0);
        builder.setReward("");

        return builder;
    }

    public static ClimbTowerProto.CrossClimbTowerTopClimbRespMsg.Builder builderCrossTopClimbRespMsg(int activityId, boolean isAttack) {
        ClimbTowerProto.CrossClimbTowerTopClimbRespMsg.Builder builder = ClimbTowerProto.CrossClimbTowerTopClimbRespMsg.newBuilder();
        builder.setIsAttack(isAttack);
        builder.setActivityId(activityId);
        builder.setSelfUserId(0);
        builder.setSelfAbility("0");
        builder.setEnemyUserId(0);
        builder.setEnemyAbility("0");

        return builder;
    }

    public static ClimbTowerProto.ClimbTowerTopLastDefeatedRespMsg.Builder buildLastDefeatMsg(UserClimbTowerTopClimbRecord record) {
        ClimbTowerProto.ClimbTowerTopLastDefeatedRespMsg.Builder defeatMsg = ClimbTowerProto.ClimbTowerTopLastDefeatedRespMsg.newBuilder();
        defeatMsg.setServerId(record.getEnemyBaseInfo().getServerId());
        defeatMsg.setEnemyName(record.getEnemyBaseInfo().getNickName());
        defeatMsg.setOldCellNum(record.getOldCellNum());
        defeatMsg.setNewCellNum(record.getNewCellNum());
        defeatMsg.setRecrodTime(record.getRecordTime());

        return defeatMsg;
    }

    public static LogClimbTowerTopFight buildLogClimbTowerTopFight(ClimbTowerProto.CrossClimbTowerTopClimbRespMsg msg) {
        LogClimbTowerTopFight log = new LogClimbTowerTopFight(
                msg.getActivityId(),
                msg.getSelfUserId(),
                msg.getSelfAbility(),
                msg.getRetClient().getNewTopCell(),
                msg.getEnemyUserId(),
                msg.getEnemyAbility(),
                msg.getRetClient().getReward()
        );

        return log;
    }
}
